package com.zzh.myapplication.activity;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import net.icarplus.car.R;
import net.icarplus.car.constant.UrlValues;
import net.icarplus.car.manager.BaseActivity;
import net.icarplus.car.tools.Encrypt;
import net.icarplus.car.tools.JsonUtil;
import net.icarplus.car.tools.LoadingUtil;
import net.icarplus.car.tools.S;
import net.icarplus.car.tools.SPUtils;
import net.icarplus.car.tools.T;
import net.icarplus.car.tools.ToolBarHelper;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

public class RegisterActivity extends BaseActivity {

    private Toolbar toolbar;
    private EditText phoneEdit, secureCodeEdit, passwordEdit, confirmPasswordEdit;
    private Button registerBtn;
    private TextView tv_get_secure_code;

    private String phone;
    private String secureCode;
    private String password;
    private String confirmPassword;

    private LoadingUtil loadingUtil;

    @Override
    public void setContent() {
        setContentView(R.layout.activity_register);
    }

    @Override
    public void initComponent() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        ToolBarHelper.setNormalTitleAndBack(this, toolbar, getString(R.string.register_now));

        phoneEdit = (EditText) findViewById(R.id.register_phone_edit);
        secureCodeEdit = (EditText) findViewById(R.id.register_secure_code_edit);
        passwordEdit = (EditText) findViewById(R.id.register_password_edit);
        confirmPasswordEdit = (EditText) findViewById(R.id.register_confirm_password_edit);
        registerBtn = (Button) findViewById(R.id.register_register_btn);
        tv_get_secure_code = (TextView) findViewById(R.id.tv_get_secure_code);
        loadingUtil = new LoadingUtil(this, "注册中...");

    }

    @Override
    public void setListener() {
        registerBtn.setOnClickListener(this);
        secureCodeEdit.setOnClickListener(this);
        tv_get_secure_code.setOnClickListener(this);

    }

    @Override
    public void onAfterInit() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.register_register_btn:
                if (validInput()) {
                    submitRegister();
                }
                break;
            case R.id.tv_get_secure_code:
                initParams();
                if (!S.isEmpty(phone) && S.isMobileNumber(phone)) {
                    getSecureCode();
                } else {
                    T.showShort(getApplicationContext(), "请输入正确的手机号");
                }
                break;
            default:
                break;
        }

    }

    private void getSecureCode() {
        JSONObject json = new JSONObject();

        try {
            json.put("mobile", phone);
            json.put("verifyType", "01");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        loadingUtil = new LoadingUtil(this, "获取验证码...");
        this.loadingUtil.startShowLoading();
        OkHttpUtils.get().url(UrlValues.GET_SECURE_CODE).addParams("input", json.toString()).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                if (JsonUtil.isValid(response)) {
                    RegisterActivity.this.loadingUtil.stopShowLoading();
                    T.showShort("验证码已发送");
                }


            }
        });
    }


    private void submitRegister() {
        JSONObject json = new JSONObject();
        try {
            json.put("accRegistChannel", "01");
            json.put("password", Encrypt.getMD5(password));
            json.put("mobile", phone);
            json.put("verifyCode", secureCode);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        loadingUtil = new LoadingUtil(this, "注册中...");
        this.loadingUtil.startShowLoading();
        OkHttpUtils.post().url(UrlValues.USER_REGISTER).addParams("input", json.toString()).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e) {
                RegisterActivity.this.loadingUtil.stopShowLoading();
                T.showShort("返回数据有误!");
            }

            @Override
            public void onResponse(String response) {
                RegisterActivity.this.loadingUtil.stopShowLoading();
                if (JsonUtil.isValid(response)) {
                    SPUtils.saveString(RegisterActivity.this, "userPasswd", Encrypt.getMD5(password));
                    Intent intent = new Intent(RegisterActivity.this, CertificateUploadActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    start_Activity(intent);
                }

            }
        });
    }

    private void initParams() {
        phone = phoneEdit.getText().toString().toString().trim();
        secureCode = secureCodeEdit.getText().toString().trim();
        password = passwordEdit.getText().toString().trim();
        confirmPassword = confirmPasswordEdit.getText().toString().trim();
    }


    private boolean validInput() {
        initParams();
        int index = 0;
        boolean isShow = false;
        if (!S.isEmpty(phone) && S.isMobileNumber(phone)) {
            index++;
        } else {
            if (!isShow) {
                T.showShort(getApplicationContext(), "请输入正确的手机号");
                isShow = true;
            }
        }
        if (S.isPwd(password)) {
            index++;
        } else {
            if (!isShow) {
                T.showShort(getApplicationContext(), "请输入6位以上数字或者字母组合成的密码");
                isShow = true;
            }
        }
        if (password.equalsIgnoreCase(confirmPassword)) {
            index++;
        } else {
            if (!isShow) {
                T.showShort(getApplicationContext(), "两次输入的密码不一样，请重新输入");
                isShow = true;
            }
        }

        if (index == 3) {
            return true;
        }
        return false;
    }

}
